﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SEO.Business;
using SEO.Entity;

public partial class DoArticleSEO : AdminPage
{


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitData();
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.ArticleSEO;
    }

    protected void InitData()
    {
        DataTable dtSort = GetAllSort();
        ddlSort.Items.Clear();
        ddlSort.Items.Add(new ListItem("请选择分类", "0"));
        foreach (DataRow drSort in dtSort.Rows)
        {
            ddlSort.Items.Add(new ListItem(drSort["Name"].ToString(), drSort["ID"].ToString()));
        }
        dtSort.Dispose();
    }

    /// <summary>
    /// 提交操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (ddlSort.SelectedValue == "0")
        {
            Until.Alert("请选择分类！");
            return;
        }
        if (string.IsNullOrEmpty(txtUrl.Text.Trim()))
        {
            Until.Alert("请输入文章网址！");
            return;
        }
        int nType = Convert.ToInt32(ddlUrlType.SelectedValue);
        string[] urlList = txtUrl.Text.Trim().Split('\n');
        if (urlList.Length > 20)
        {
            Until.Alert("文章网址最多输入20行！");
            return;
        }

        //获取网址
        BisArticleSEO bisInfo = new BisArticleSEO();
        BisWebSEO bisWebSEO = new BisWebSEO();
        DateTime dCreateTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
        DateTime dCreateTime1 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 7:00:00"));
        List<string> articleUrlList = new List<string>();
        for (int i = 0; i < urlList.Length; i++)
        {
            string strArticelesUrl = urlList[i].Trim().Replace(@"\", "/");
            if (strArticelesUrl.IndexOf("http://") < 0 && strArticelesUrl.IndexOf("https://") < 0)
                strArticelesUrl = "http://" + strArticelesUrl;
            string strTemUrl = strArticelesUrl;
            //去"/"
            if (strArticelesUrl.Substring(strArticelesUrl.Length - 1, 1) == "/")
            {
                strTemUrl = strArticelesUrl.Substring(0, strArticelesUrl.Length - 1);
            }
            if (string.IsNullOrEmpty(strTemUrl))
            {
                Until.Alert("第" + (i + 1) + "行网址不能为空。");
                return;
            }
            if (strTemUrl.Length > 200)
            {
                Until.Alert("第" + (i + 1) + "行网址不能超过200个字符。");
                return;
            }
            //验证是否已经输入
            if (bisInfo.CheckArticlesUrl(0, strTemUrl))
            {
                Until.Alert("网址已经存在，请不要重复输入。");
                return;
            }

            articleUrlList.Add(strArticelesUrl);
        }
        //过滤重复的
        articleUrlList = articleUrlList.Distinct<string>().ToList();
        List<ArticleSEO> infoList = new List<ArticleSEO>();
        int nSortID = Convert.ToInt32(ddlSort.SelectedValue);
        BisNewsWeb bisNewsWeb = new BisNewsWeb();
        BisOurWeb bisOurWeb = new BisOurWeb();
        if (nType == 0)
        {
            DataRow[] dtKeywords = GetKeywordsListBySortID(nSortID);
            if (dtKeywords != null && dtKeywords.Length > 0)
            {
                //验证关键字
                for (int i = 0; i < articleUrlList.Count; i++)
                {
                    string strTitle = HtmlTool.GetTitle(articleUrlList[i]);
                    if (string.IsNullOrEmpty(strTitle))
                    {
                        Until.Alert("获取网址" + articleUrlList[i] + "标题失败，请确认该网址是否可以正常访问。");
                        return;
                    }
                    if (articleUrlList[i].Substring(articleUrlList[i].Length - 1, 1) == "/")
                    {
                        articleUrlList[i] = articleUrlList[i].Substring(0, articleUrlList[i].Length - 1);
                    }
                    //文章对象
                    ArticleSEO Info = new ArticleSEO();
                    Info.SortID = nSortID;
                    Info.AritcleTitle = strTitle;
                    Info.ArticleUrl = articleUrlList[i];
                    Info.AdminID = nAdminUserID;
                    Info.UrlType = 2;
                    Info.Status = false;
                    bool bTag = false;
                    //匹配关键字
                    foreach (DataRow drKeywords in dtKeywords)
                    {
                        if (strTitle.IndexOf(drKeywords["Name"].ToString()) >= 0)
                        {
                            if (Convert.ToBoolean(drKeywords["SysLock"]) || Convert.ToBoolean(drKeywords["DiyLock"]))
                            {
                                Until.Alert(articleUrlList[i] + "的标题匹配到的关键字\"" + drKeywords["Name"].ToString() + "\"被锁定。");
                                return;
                            }
                            bTag = true;
                            //关键字1
                            if (Info.KeywordsID == null)
                            {
                                Info.KeywordsID = Convert.ToInt32(drKeywords["ID"]);
                                Info.Keywords = drKeywords["Name"].ToString();
                                strTitle = strTitle.Replace(Info.Keywords, "");
                                continue;
                            }
                            //关键字2
                            if (Info.KeywordsID > 0 && Info.KeywordsID1 == null)
                            {

                                Info.KeywordsID1 = Convert.ToInt32(drKeywords["ID"]);
                                Info.Keywords1 = drKeywords["Name"].ToString();
                                break;
                            }
                        }
                    }

                    if (!bTag)
                    {
                        Until.Alert(articleUrlList[i] + "的标题没有匹配到指定分类下的关键字。");
                        return;
                    }
                    infoList.Add(Info);
                }
            }
        }
        else if (nType == 1)
        {

            //验证是否站内网址
            for (int i=0;i<articleUrlList.Count;i++)
            {
                string strTitle = HtmlTool.GetTitle(articleUrlList[i]);
                if (string.IsNullOrEmpty(strTitle))
                {
                    Until.Alert("获取网址" + articleUrlList[i] + "标题失败，请确认该网址是否可以正常访问。");
                    return;
                }
                if (articleUrlList[i].Substring(articleUrlList[i].Length - 1, 1) == "/")
                {
                    articleUrlList[i] = articleUrlList[i].Substring(0, articleUrlList[i].Length - 1);
                }
                //文章对象
                ArticleSEO Info = new ArticleSEO();
                Info.SortID = nSortID;
                Info.AritcleTitle = strTitle;
                Info.ArticleUrl = articleUrlList[i];
                Info.AdminID = nAdminUserID;
                Info.Status = false;

                //文章类型
                string strTopDomain = "";
                strTopDomain = Common.GetTopDomain(articleUrlList[i]);
                int nAimID = bisOurWeb.GetOurWebID(strTopDomain);
                if (nAimID > 0)
                {
                    //站内类型
                    Info.UrlType = 3;
                    Info.AimID = nAimID;
                }
                else
                {
                    Until.Alert(articleUrlList[i] + "不是站内网址。");
                    return;
                }
                infoList.Add(Info);
            }
        }

        //非站内获取文章类型
        if (nType == 0)
        {
            foreach (ArticleSEO Info in infoList)
            {
                //获取文章类型
                int nAimID = Common.CheckNewsWeb(Info.ArticleUrl);
                if (nAimID > 0)
                {
                    //验证是否在今天之前已经百度已经收录
                    if (bisWebSEO.CheckWebSEO(Info.ArticleUrl, dCreateTime.AddDays(-7)))
                    {
                        Until.Alert("文章网址在7天之前已经被百度收录，请不要重复输入。");
                        return;
                    }
                    //新闻源类型
                    Info.UrlType = 1;
                    Info.AimID = nAimID;
                }
                else
                {
                    //验证是否在今天之前已经百度已经收录
                    if (bisWebSEO.CheckWebSEO(Info.ArticleUrl, dCreateTime1))
                    {
                        Until.Alert("文章网址在今天7:00之前已经被百度收录，请不要重复输入。");
                        return;
                    }
                }
            }
        }


        bool bResult = false;
        try
        {
            //批量新增
            foreach (ArticleSEO Info in infoList)
            {
                bisInfo.Insert(Info);
            }
            bResult = true;
        }
        catch (Exception ex)
        {
            bResult = false;
            Common.Log("新增文章失败" + ex.Message);
        }

        //结果处理
        if (bResult)
        {
            Until.AlertJS("提交成功！", "ResetPage();");
        }
        else
        {
            Until.Alert("提交失败！");
        }
    }
}